<template>
  <header class="nav-header">
    <div class="nav-container">
      <div class="nav-left">
        <router-link to="/" class="logo"> 养老服务平台 </router-link>
        <nav v-if="!isStaff" class="nav-menu">
          <router-link to="/" class="nav-item" :exact-active-class="'router-link-exact-active'">首页</router-link>
          <router-link to="/consultation" class="nav-item" active-class="router-link-active">问诊服务</router-link>
          <router-link to="/events" class="nav-item" active-class="router-link-active">活动中心</router-link>
          <router-link to="/courses" class="nav-item" active-class="router-link-active">课程中心</router-link>
          <router-link to="/knowledge" class="nav-item" active-class="router-link-active">养老知识</router-link>
        </nav>
      </div>
      <div class="nav-right">
        <el-dropdown trigger="click" @command="handleCommand">
          <div class="user-info">
            <el-avatar :size="32" :icon="UserFilled" />
            <span class="username">{{ userStore.user?.username }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <template v-if="isStaff">
                <el-dropdown-item command="staff">管理后台</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </template>
              <template v-else>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </header>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { UserFilled } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const isStaff = computed(() => {
  return userStore.user?.role === 'admin' || userStore.user?.role === 'nurse'
})

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'staff') {
    router.push('/staff')
  } else if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      userStore.logout()
      router.push('/login')
    } catch (error) {
      // 用户取消退出
    }
  }
}
</script>

<style scoped>
.nav-header {
  background: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 64px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 40px;
}

.logo {
  font-size: 20px;
  font-weight: 600;
  color: var(--el-color-primary);
  text-decoration: none;
}

.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-item {
  color: #2c3e50;
  text-decoration: none;
  font-size: 15px;
  padding: 8px 0;
  position: relative;
  transition: color 0.3s ease;
}

.nav-item:hover {
  color: var(--el-color-primary);
}

.nav-item.router-link-active {
  color: var(--el-color-primary);
}

.nav-item.router-link-active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: var(--el-color-primary);
}

.nav-item.router-link-exact-active {
  color: var(--el-color-primary);
}

.nav-item.router-link-exact-active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: var(--el-color-primary);
}

.nav-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  color: #2c3e50;
  font-size: 14px;
}

@media (max-width: 768px) {
  .nav-menu {
    display: none;
  }

  .logo {
    font-size: 18px;
  }

  .username {
    display: none;
  }
}
</style>
